using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DTLAutomateGUI.Data;

namespace DTLAutomateGUI.Controls.FormControls
{
	public partial class BorderPropertiesGroupBox : UserControl
	{
		public BorderPropertiesGroupBox()
		{
			InitializeComponent();
		}

		public BorderPropertiesGroupBox(DataSetLayout.BorderRow row)
		{
			InitializeComponent();
			this.DataRow = row;
		}

		private DataSetLayout.BorderRow dataRow;

		public DataSetLayout.BorderRow DataRow
		{
			get { return dataRow; }
			set
			{
				dataRow = value;
				dataRow.Table.RowChanged += new DataRowChangeEventHandler(Table_RowChanged);
				this.repopulateFields();
			}
		}

		void Table_RowChanged(object sender, DataRowChangeEventArgs e)
		{
			if (e.Row == this.dataRow)
			{
				this.repopulateFields();
			}
		}

		private void repopulateFields()
		{
			nameTextBox.Text = dataRow.name;
			typeTextBox.Text = dataRow.type;
			locationXTextBox.Text = dataRow.locationX.ToString();
			locationYTextBox.Text = dataRow.locationY.ToString();
			blockAIdTextBox.Text = dataRow.blockAId.ToString();
			blockBIdTextBox.Text = dataRow.blockBId.ToString();
			sensorStateComboBox.Text = dataRow.stateOfSensor;
			signalStateComboBox.Text = dataRow.stateOfSignal;
			networkAddressTextBox.Text = dataRow.networkAddress.ToString();
		}

		private void nameTextBox_TextChanged(object sender, EventArgs e)
		{
			dataRow.name = nameTextBox.Text;
			dataRow.AcceptChanges();
		}

		private void typeTextBox_TextChanged(object sender, EventArgs e)
		{
			dataRow.type = typeTextBox.Text;
			dataRow.AcceptChanges();
		}

		private void locationXTextBox_TextChanged(object sender, EventArgs e)
		{
			dataRow.locationX = Int32.Parse(locationXTextBox.Text);
			dataRow.AcceptChanges();
		}

		private void locationYTextBox_TextChanged(object sender, EventArgs e)
		{
			dataRow.locationY = Int32.Parse(locationYTextBox.Text);
			dataRow.AcceptChanges();
		}

		private void sensorStateComboBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			dataRow.stateOfSensor = sensorStateComboBox.Text;
			dataRow.AcceptChanges();
		}

		private void signalStateComboBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			dataRow.stateOfSignal = signalStateComboBox.Text;
			dataRow.AcceptChanges();
		}

		private void networkAddressTextBox_TextChanged(object sender, EventArgs e)
		{
			dataRow.networkAddress = Int32.Parse(networkAddressTextBox.Text);
			dataRow.AcceptChanges();
		}

		private void blockAIdTextBox_TextChanged(object sender, EventArgs e)
		{
			dataRow.blockAId = Int32.Parse(blockAIdTextBox.Text);
			dataRow.AcceptChanges();
		}

		private void blockBIdTextBox_TextChanged(object sender, EventArgs e)
		{
			dataRow.blockBId = Int32.Parse(blockBIdTextBox.Text);
			dataRow.AcceptChanges();
		}

		private void nameTextBox_Enter(object sender, EventArgs e)
		{
			this.nameTextBox.Focus();
		}


	}
}
